package com.fastloan.core.apiauth.base.service;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 加解密接口
 *
 * @author yanwe
 * createTime 2018-01-2018/1/4 10:31
 */
public abstract class GeneralEncrypt {

    static {
        java.security.Security.addProvider(
                new org.bouncycastle.jce.provider.BouncyCastleProvider()
        );
    }

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 密钥
     */
    private String key;

    private String aesKey;

    private String aesIv;

    private String rsaPubKey;

    private String rsaPriKey;

    public abstract String signBean(Object object,String key);

    public abstract String encryptBean(Object object,String key);

    public abstract String decodeBean(String data);

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }

    public String getAesIv() {
        return aesIv;
    }

    public void setAesIv(String aesIv) {
        this.aesIv = aesIv;
    }

    public String getRsaPubKey() {
        return rsaPubKey;
    }

    public void setRsaPubKey(String rsaPubKey) {
        this.rsaPubKey = rsaPubKey;
    }

    public String getRsaPriKey() {
        return rsaPriKey;
    }

    public void setRsaPriKey(String rsaPriKey) {
        this.rsaPriKey = rsaPriKey;
    }

    public Map<String,Object> getBeanMap(Object object){
        Map<String, Object> beanMap = null;
        try {
            beanMap = PropertyUtils.describe(object);
            beanMap.remove("class");
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return beanMap;
    }
}
